<?php
namespace Home\Controller;
use Think\Controller;
use Think;


class  PhotoController extends Controller {

    public function index(){
        echo "hello";
    }


    public function upload() { 
        if (!empty($_FILES)) { 
            //如果有文件上传 上传附件 
            $this->_upload(); 
            //$this->forward(); 
        }
    }

    // 文件上传 
    public function _upload($path) {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =    3145728 ;// 设置附件上传大小
        $upload->exts      =    array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =    './Public/'; // 设置附件上传根目录
        $upload->savePath  =    $path; // 设置附件上传（子）目录
        $upload->autoSub   =    false;
        // 上传文件 
        $info   =   $upload->upload();
        return $info;
    }


    public function uploadPhoto(){

        $path = './Photo/';
        if (!empty($_FILES)) { 
            $upload = $this->_upload($path); 
        }
        if(!$upload) {
            $array= array('success'=>0, 'error'=>$upload->getError());
            echo json_encode($array);
        }else{// 上传成功
            $data['photo_name'] = $upload['photo']['savename'];
            $data['photo_take_time'] = date('Y-m-d H:i:s');
            $data['photo_owner_device_id'] = I('device_ID');
            $photo = M('photo');
            $folder_id = I('folder_id');
            $data['folder_id'] = $folder_id;

            $image = new \Think\Image(); 
            $image->open('./Public/Photo/'.$data['photo_name']);
            // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
            $image->thumb(150, 150)->save('./Public/Thumb/'.$data['photo_name']);

            $res = $photo->add($data);

            $db_folder = M('folder');
            $db_folder->where("folder_id = '%d'", $folder_id)->setInc('count', 1);
            
            if($res){
                header('Location: http://128.199.209.239/IM/index.php/Home/Console/photoEdit/folder_id/'.$folder_id);
            } else{
                $array= array('success'=>0, 'error'=>"上传失败");
                echo json_encode($array);
            }
        }
    }

    public function getList(){
        $folder_id = I('folder_id');
        $db_photo = M('photo');
        $res = $db_photo->where("folder_id='%d'", $folder_id)->order('update_time desc')->select();
        echo json_encode($res);
    }


    public function getFolders(){
        $db_folder = M('folder');
        $res = $db_folder->join("left join photo on folder.cover_id=photo.photo_id")->select();
        echo json_encode($res);
    }

    public function delete(){
        $db_photo = M('photo');
        $photo_id = I('photo_id');
        $folder_id = I('folder_id');
        $db_folder = M('folder');

        $db_folder->where("folder_id='%d'", $folder_id)->setInc('count', -1);
        $photo = $db_photo->where("photo_id = '%d'", $photo_id)->find();

        $res = $db_photo->where("photo_id = '%d'", $photo_id)->delete();
        unlink('./Public/Thumb/'.$photo['photo_name']);
        unlink('./Public/Photo/'.$photo['photo_name']);
        if($res){
            echo json_encode($path);
        }else{
            echo json_encode('删除失败');
        }
    }



    public function folderEdit(){
        $db_folder = D('folder');
        if(!$db_folder->create()){
            $array = array('success'=>0, 'error'=>$db_folder->getError());
            exit(json_encode($array));
        }else{
            $db_folder->save();
            $array = array('success'=>1);
            exit(json_encode($array));
        }
    }

    public function folderDelete(){
        $db_folder = M('folder');
        $folder_id = I('folder_id');
        $db_folder->where("folder_id = '%d'", $folder_id)->delete();
        echo json_encode(array('success'=>1));
    }


    public function setCover(){
        $db_folder = M('folder');
        $folder_id = I('folder_id');
        $photo_id = I('photo_id');
        $db_folder->where("folder_id='%d'", $folder_id)->setField('cover_id', $photo_id);
        echo json_encode(array('success'=>1));
    }

    public function folderAdd(){
        $db_folder = D('folder');
        if(!$db_folder->create()){
            $array = array('success'=>0, 'error'=>$db_folder->getError());
            exit(json_encode($array));
        }else{
            $db_folder->add();
            $array = array('success'=>1);
            exit(json_encode($array));
        }
    }
}